<template>
    <div>
        <Form :label-width="100" inline>
            <Form-item label="区划">
                <dictSelect :sel_val='queryParam.cfad0002' dicType="service"
                            dictService="/api/ac/subsistence/extSysDictService/getSs01001List" clearable
                            :bindData.sync="queryParam.cfad0002" style="width:180px"/>
            </Form-item>
            <Form-item label="救助类别">
                <dictSelect :sel_val='queryParam.cfad0001' dictCode="assistancetype" style="width: 180px"
                            :bindData.sync="queryParam.cfad0001" clearable/>
            </Form-item>
            <Form-item label="银行名称">
                <dictSelect :sel_val='queryParam.cfad0003' dictCode="bankcategory" style="width: 180px"
                            :bindData.sync="queryParam.cfad0003" clearable/>
            </Form-item>
            <Row class="button_row">
                <span style="float: left;">
                <Button type="primary" @click="handleAdd()">录入</Button>
                </span>
                <span style="float: right;">
                      <Button type="primary" @click="searchQuery">查询</Button>
                <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
                </span>
            </Row>
        </Form>
        <Modal v-model="visible" :title="title" width="750" style="position:relative;">
            <Form ref="formData" :model="formData" :label-width="150" :rules="rules" :disabled="disableSubmit">
                <table>
                    <tr>
                        <td>
                            <FormItem label="行政区划" prop="cfad0002">
                                <dictSelect :sel_val='formData.cfad0002' dicType="service"
                                            dictService="/api/ac/subsistence/extSysDictService/getSs01001List" clearable
                                            :bindData.sync="formData.cfad0002" style="width:200px"/>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="救助类别" prop="cfad0001">
                                <dictSelect :sel_val='formData.cfad0001' dictCode="assistancetype"
                                            :bindData.sync="formData.cfad0001" clearable style="width: 200px"/>
                            </FormItem>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <FormItem label="银行名称" prop="cfad0003">
                                <dictSelect :sel_val='formData.cfad0003' dictCode="bankcategory" style="width: 200px"
                                            :bindData.sync="formData.cfad0003" clearable/>
                            </FormItem>
                        </td>
                        <td>
                            <FormItem label="排序号" prop="cfad0006">
                                <Input v-model="formData.cfad0006" style="width: 200px"></Input>
                            </FormItem>
                        </td>
                    </tr>
                </table>
            </Form>
            <div slot="footer">
                <Button type="text" size="large" @click="visible = false">取消</Button>
                <template v-if="!disableSubmit">
                    <Button type="primary" size="large" @click="handleSubmit('formData')">确定</Button>
                </template>
            </div>
        </Modal>
        <div class="table_div">
            <Table :columns="columns" :data="tableData" :loading="Loading">
                <template slot-scope="{ row, record, index  }" slot="action">
                    <Button type="info" size="small" style="margin-right: 5px" @click="handleEdit(index)">编辑</Button>
                    <Button type="success" size="small" style="margin-right: 5px" @click="handleDetail(index)">详情
                    </Button>
                    <Button type="error" size="small" style="margin-right: 5px" @click="handleDelete(row.id)">删除
                    </Button>
                </template>
            </Table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
        <rule-action-infor-modal ref="modalForm" @ok="modalFormOk"/>
    </div>
</template>

<script>
    import {postAction} from '@/api/request'
    import {CommonMixin} from '@/views/common/js/CommonMixin'
    import dictSelect from '@/components/dictSelect/dictSelect.vue'


    export default {
        mixins: [CommonMixin],
        components: {
            dictSelect,
        },
        data() {
            return {
                rules: {
                    cfad0002: [
                        {required: true, message: '请选择"行政区划"', trigger: 'blur'}
                    ],
                    cfad0001: [
                        {required: true, message: '请选择"救助类别"', trigger: 'blur'}
                    ],
                    cfad0003: [
                        {required: true, message: '请选择"银行名称"', trigger: 'blur'}
                    ],
                    cfad0006: [
                        {required: true, message: '请输入"排序号"', trigger: 'blur'}
                    ]
                },
                fieldsToValidate02: ["cfad0002", "cfad0001", "cfad0003", "cfad0006"],
                columns: [
                    {
                        title: '区县名称',
                        align: 'center',
                        key: 'cfad0002_name'
                    },
                    {
                        title: '救助类别',
                        align: 'center',
                        key: 'cfad0001_name'
                    },
                    {
                        title: '银行名称',
                        align: 'center',
                        key: 'cfad0003_name'
                    },
                    {
                        title: '排序号',
                        align: 'center',
                        key: 'cfad0006'
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        slot: 'action',
                        width: '200px'
                    }
                ],
                tableData: [],
                Loading: false,
                url: {
                    list: "/api/ac/subsistence/ss06003Service/getPageList",
                    delete: "/api/ac/subsistence/ss06003Service/deleteData"
                },
                formData: {},
                queryParam: {},
                visible: false,
                dataCount: 0,
                pageSize: 10,
                modal: false,
                title: "",
                disableSubmit: false
            }
        },
        methods: {
            handleAdd() {
                this.$refs['formData'].resetFields();
                this.disableSubmit = false;
                let that = this;
                that.title = "资金发放配置录入"
                let record = {};
                that.visible = true
                that.formData = Object.assign({}, record);
                that.formData.cfad0006 = null;
            },
            handleDetail(index) {
                this.$refs['formData'].resetFields();
                let that = this;
                that.title = "详情";
                that.disableSubmit = true;
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.formData = Object.assign({}, record)
                } else {
                    that.visible = false
                }
            },
            handleEdit(index) {
                this.$refs['formData'].resetFields();
                this.disableSubmit = false;
                let that = this;
                that.title = "资金发放配置编辑"
                let record = that.tableData[index];
                if (record.id) {
                    that.visible = true
                    that.formData = Object.assign({}, record)
                } else {
                    that.visible = false
                }
            },
            handleSubmit(name) {
                let that = this;
                let validate = true;
                let err_msg = [];

                for (let item of that.fieldsToValidate02) {
                    that.$refs[name].validateField(item, (error) => {
                        if (error) {
                            validate = false;
                            err_msg.push(that.rules[item][0].message);
                        }
                    })
                }
                if (validate) {
                    let that = this;
                    that.Loading = true;
                    let url = "/api/ac/subsistence/ss06003Service/insertData";
                    if (this.formData.id) {
                        url = "/api/ac/subsistence/ss06003Service/updateData";
                    }
                    postAction("/api/ac/subsistence/ss06003Service/check", this.formData).then((res) => {
                        if (res.data.errcode != 0) {
                            this.$Message.error(res.data.errmsg);
                        } else {
                            // 保存数据前先检查 该区划 是否存在 相同救助类型 的配置
                            if (res.data.data.Message === "false") {
                                this.$Message.error("该区划存在相同配置");
                                return;
                            } else {
                                postAction(url, this.formData).then((res) => {
                                    if (res.data.errcode != 0) {
                                        this.$Message.error(res.data.errmsg);
                                    } else {
                                        this.modalFormOk();
                                        this.visible = false;
                                    }
                                }).finally(() => {
                                    that.Loading = false;
                                });
                            }
                        }
                    }).finally(() => {
                        that.Loading = false;
                    });
                } else {
                    that.$Message.error(err_msg[0]);
                    return;
                }
            }
        },
        created() {
            this.loadData();
        }
    }
</script>
<style scoped>
    /deep/ .ivu-form-item-error-tip {
        display: none;
    }

    .ivu-form-item {
        margin-bottom: 0px;
    }

    .itemStyle {
        width: 180px
    }

    .head-img1 {
        height: 200px;
        width: 250px;
        margin-top: 15px;
    }

    .head-img {
        height: 200px;
        width: 250px;
    }

    .imgBackColor {
        background-color: WhiteSmoke;
        height: 250px;
    }

    .positiveTDiv {
        height: 240px;
        width: 75px;
        line-height: 240px;
        background-color: #7CCD7C;
        margin-bottom: 10px;
        color: white;
        font-size: 14px;
        font-weight: 700;
    }
</style>
